package com.ysstech.reportworld.service.impl;

import com.ysstech.common.util.CollectionUtil;
import com.ysstech.reportworld.entity.DataModular;
import com.ysstech.reportworld.mapper.DataModularMapper;
import com.ysstech.reportworld.service.DataModularService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 基础数据对应模块信息 服务实现类
 * </p>
 *
 * @author lishuangliang
 * @since 2022-12-02
 */
@Service
public class DataModularServiceImpl extends ServiceImpl<DataModularMapper, DataModular> implements DataModularService {

    @Override
    public Map<String, List<String>> getDataModularMap() throws Exception {
        // 所属模块 code和数据，对应一个list
        List<DataModular> modularList = this.list();
        if (CollectionUtils.isEmpty(modularList)) {
            return null;
        }
        Map<String, List<String>> mapDataModular = new HashMap<>();
        for (DataModular modular : modularList) {
            if (null == modular || StringUtils.isBlank(modular.getDataId()) || StringUtils.isBlank(modular.getDataCode())) {
                continue;
            }
            String key = modular.getDataCode() + "\t" + modular.getDataId();
            if (mapDataModular.containsKey(key)) {
                List<String> list = mapDataModular.get(key);
                list.add(modular.getModularId());
                mapDataModular.put(key, list);
            } else {
                List<String> list = new ArrayList<>();
                list.add(modular.getModularId());
                mapDataModular.put(key, list);
            }
        }
        return mapDataModular;
    }
}
